<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  ~ Copyright (c) 2017 Contributors to the Eclipse Foundation
  ~
  ~ See the NOTICE file(s) distributed with this work for additional
  ~ information regarding copyright ownership.
  ~
  ~ This program and the accompanying materials are made available under the
  ~ terms of the Eclipse Public License 2.0 which is available at
  ~ http://www.eclipse.org/legal/epl-2.0, or the Apache Software License 2.0
  ~ which is available at https://www.apache.org/licenses/LICENSE-2.0.
  ~
  ~ SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<div class="localLoader" [class.hidden]="!loading">
    <winery-loader></winery-loader>
</div>

<div *ngIf="!loading">
    <winery-table [data]="policies" [columns]="columnsArray"
                  (addBtnClicked)="add()" (cellSelected)="selected($event.row)" (removeBtnClicked)="remove()">
    </winery-table>
</div>

<winery-modal bsModal #addModal="bs-modal" [modalRef]="addModal">
    <winery-modal-header [title]="'Add Policy'">
    </winery-modal-header>
    <winery-modal-body>
        <form #addForm="ngForm" id="addForm">
            <div class="form-group">
                <label class="control-label" for="policyName">Name:</label>
                <input class="form-control" id="policyName" name="name"
                       [(ngModel)]="newPolicy.name" #nameInput="ngModel"
                       [wineryDuplicateValidator]="validator" required>
                <div *ngIf="nameInput.errors && (nameInput.dirty || nameInput.touched)"
                     class="alert alert-danger">
                    <div [hidden]="!nameInput.errors.wineryDuplicateValidator">
                        No duplicates allowed!
                    </div>
                    <div [hidden]="!nameInput.errors.required">
                        Name is required!
                    </div>
                </div>
            </div>

            <div class="form-group">
                <label class="control-label" for="policyType">Policy Type</label>
                <ng-select id="policyType"
                           [items]="policyTypes" [active]="[activePolicyType]"
                           (selected)="policyTypeSelected($event)">
                </ng-select>
            </div>

            <div class="form-group">
                <div *ngIf="!loadingTemplate">
                    <label class="control-label" for="policyTemplate">Policy Template</label>
                    <ng-select id="policyTemplate"
                               [items]="policyTemplates" [active]="[activePolicyTemplate]"
                               (selected)="policyTemplateSelected($event)">
                    </ng-select>
                </div>

                <div [hidden]="!loadingTemplate">
                    <winery-loader></winery-loader>
                </div>
            </div>

            <div class="form-group">
                <winery-instance-edit-xml
                    #xmlEditor
                    [getXmlData]="false" [hideSaveButton]="true" [xmlData]="policyXml">
                </winery-instance-edit-xml>
            </div>
        </form>
    </winery-modal-body>
    <winery-modal-footer (onOk)="addConfirmed();"
                         [disableOkButton]="!addForm?.form.valid"
                         [okButtonLabel]="'Add'">
    </winery-modal-footer>
</winery-modal>

<winery-modal bsModal #confirmDeleteModal="bs-modal" [modalRef]="confirmDeleteModal">
    <winery-modal-header [title]="'Delete Policy'">
    </winery-modal-header>
    <winery-modal-body>
        <p *ngIf="selectedCell != null" id="diagyesnomsg">
            Do you want to delete the Element <span style="font-weight:bold;">{{ selectedCell.name }}</span>?
        </p>
    </winery-modal-body>
    <winery-modal-footer (onOk)="removeConfirmed();"
                         [okButtonClass]="'btn-danger'"
                         [okButtonLabel]="'Delete'">
    </winery-modal-footer>
</winery-modal>
